package com.wuliaogame.utils;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wuliaogame.entity.PBisai;
import com.wuliaogame.mapper.PHeroMapper;
import com.wuliaogame.mapper.PUserMapper;
import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component

public class ExcelUtils {
    @Autowired
    PUserMapper pUserMapper;
    @Autowired
    PHeroMapper pHeroMapper;

    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelUtils.class);
    public static  ExcelUtils excelUtils;
    @PostConstruct
    public void init(){
        excelUtils = this;
        excelUtils.pUserMapper = this.pUserMapper;
        excelUtils.pHeroMapper=this.pHeroMapper;
    }
    public static List<PBisai> excelToShopIdList(InputStream inputStream) {
        List<PBisai> list = new ArrayList<>();

        Workbook workbook = null;
        try {
            workbook = WorkbookFactory.create(inputStream);
            inputStream.close();
            //工作表对象
            Sheet sheet = workbook.getSheetAt(0);
            //总行数
            int rowLength = sheet.getLastRowNum() + 1;
            //工作表的列
            Row row = sheet.getRow(0);
            //总列数
            int colLength = row.getLastCellNum();
            //得到指定的单元格
            Cell cell ;
            for (int i = 1; i < rowLength; i++) {
                row = sheet.getRow(i);
                PBisai baDeposit=new PBisai();
                for (int j = 1; j < colLength; j++) {
                    cell = row.getCell(j);
                    if (cell != null) {
                        //将数字类型转化为String
                        Object data = cell.getStringCellValue();
                       if(j==1){
                           QueryWrapper wrapper=new QueryWrapper();
                           wrapper.eq("username",data.toString());
                        baDeposit.setUserId(excelUtils.pUserMapper.selectOne(wrapper).getId());
                       }
                       if(j==2) {
                          QueryWrapper wrapper=new QueryWrapper();
                          wrapper.eq("name",data.toString());
                          baDeposit.setHeroId(excelUtils.pHeroMapper.selectOne(wrapper).getId());
                       }
                       if(j==3) {
                          baDeposit.setResult(data.toString());
                       }
                    }
                    Date date=new Date();
                    Timestamp timestamp=new Timestamp(date.getTime());
                    baDeposit.setCreatTime(timestamp);
                }
                list.add(baDeposit);
            }
        } catch (Exception e) {
            LOGGER.error("parse excel file error :", e);
        }
        return list;
    }

}
